package example;

import java.util.Calendar;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.omg.CORBA.Request;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.http.HttpParameters;

public class UpdateStatus {

    public static void main(String[] args) throws Exception {
    	System.out.println(System.getProperty("debug"));
        //OAuthConsumer consumer = new DefaultOAuthConsumer(
        //        "your_oauth_key", "your_oauth_secret");
        //consumer.setTokenWithSecret("your_access_token", "your_access_token_secret");
        OAuthConsumer consumer = new DefaultOAuthConsumer(
                "ynWzhGF30csLuVQEX4X8", "tF#393T=#uKz(5e)9CRI2k(pKyYPTooxDbBUPVYp");
        consumer.setTokenWithSecret("4cecc4499d46d4be612fc26796877299", "78247b8adca75fbb18ab67d96087397f");
        
        //要发送的微博
        String status = "From Twigetor " + Calendar.getInstance().getTime();
        String statusUTF8 = URLEncoder.encode(status,"utf-8").replaceAll("\\+", "%20");
       
        
    	URL url = new URL("http://api.t.sohu.com/statuses/update.json");
    	HttpURLConnection request = (HttpURLConnection) url.openConnection();
    	request.setDoOutput(true);
    	request.setRequestMethod("POST");
    	
    	HttpParameters para = new HttpParameters();
    	para.put("status", statusUTF8);
    	consumer.setAdditionalParameters(para);
    	
    	consumer.sign(request);
        OutputStream ot = request.getOutputStream();
        ot.write(("status="+statusUTF8).getBytes());
        ot.flush();
        ot.close();
        
    	System.out.println("Sending request...");
    	request.connect();
    	System.out.println("Response: " + request.getResponseCode() + " "
    			+ request.getResponseMessage());
		BufferedReader reader =new BufferedReader(new InputStreamReader(request.getInputStream()));
		String b = null;
		while((b = reader.readLine())!=null){
			System.out.println(b);
		}
    }
}
